import axios from 'axios'
import router from '../router/index'
// import config from '../config.json'

// axios.defaults.baseURL = config.baseURL

axios.interceptors.request.use((conf) => {
  const { localStorage } = window
  const token = localStorage.token || ''
  // eslint-disable-next-line
  conf.headers.authorization = `shop ${token}`
  return conf
})

axios.interceptors.response.use((res) => {
  if (res.data.status === 401) {
    router.replace('/')
    return Promise.reject()
  }
  return res
})

export default {
  LOGIN(loginData) {
    return axios.post('/shop/login', loginData)
  },
  SIGNUP(signupData) {
    return axios.post('/shop/signup', signupData)
  },
  GET_GOODS_BY_SHOPID({ shopId }) {
    return axios.get(`/shop/${shopId}/goods`)
  },
  GET_SHOP_INFO({ shopId }) {
    return axios.get(`/shop/${shopId}/info`)
  },
  CHECK_TOKEN() {
    return axios.post('/shop/checkToken')
  },
  // 迁移到good  api下 start
  ADD_KINDS(kinds) {
    return axios.post('/kind/add', { kinds })
  },
  ADD_GOODS(goods) {
    return axios.post('/good/add', { goods })
  },
  UPSERT(good) {
    return axios.post('/good/upsert', { good })
  },
  DELETE_GOODS(goods) {
    return axios.post('/good/delete', { goods })
  },
  DELETE_KINDS(kinds) {
    return axios.post('/kind/delete', { kinds })
  },
  // 迁移到good  api下 end
  // options 包括method pathname
  GET_UPTOKEN(options) {
    return axios.get(`/cos/auth?filename=${options.filename}&fsizeLimit=${options.fsizeLimit}`)
  },
  SAVE_SHOP_INFO(shopInfo) {
    return axios.post('/shop/editInfo', { shopInfo })
  }
}
